package servlet;

import dao.SearchDao;
import dao.daoimpl.SearchDaoImpl;
import bean.Article;
import bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name="InquireBlogServlet",urlPatterns = "/InquireBlogServlet")
public class InquireBlogServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");/*防止乱码*/
        User user=(User)request.getSession().getAttribute("user");/*获取用户*/
        String time=request.getParameter("inquireArticleTime");/*获取时间*/
        String subfield=request.getParameter("inquireArticleSubfield");/*获取分栏*/
        String type=request.getParameter("inquireArticleType");/*获取类型*/
        String label=request.getParameter("inquireArticleLabel");/*获取标签*/
        System.out.println(time+subfield+type+label);
        SearchDao searchDao=new SearchDaoImpl();/*查询接口的实现类*/
        try {
            System.out.println("系统查询中.....");
            for(Article article:searchDao.getInquireArticles(time,subfield,label,type,user)){
                System.out.println(article.getTitle());
            }
            request.getSession().setAttribute("userArticles",searchDao.getInquireArticles(time,subfield,label,type,user));
            System.out.println("系统查询完毕");
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
    }
}
